/* 
 * File:   SimmilarityClustering.cpp
 * Author: Claudio
 * 
 * Created on February 26, 2013, 2:13 AM
 */

#include "SimmilarityClustering.h"

namespace AAM{
SimmilarityClustering::SimmilarityClustering() {
}

SimmilarityClustering::SimmilarityClustering(const SimmilarityClustering& orig) {
}

SimmilarityClustering::~SimmilarityClustering() {
}

vector<Event> SimmilarityClustering::doCluster(vector<Imagens*> &allImages){
        vector<pair<double,int> > simDiffs;
        for (int a = 0; a < allImages.size(); a++) {
            //                out1 << endl << allImages[a]->getTime();
            if (a > 0) {
                double simDiffVal = -log10(histComp->compImHist(allImages[a], allImages[a - 1]) + 1);
                
                simDiffs.push_back(pair<double,int>(simDiffVal,a));
                //                    out1 << " " << timeDiffVal;
            }
        }
        vector<Event> evS = searchForEvents(simDiffs,0);    
        return evS;
}
bool SimmilarityClustering::compareImagens(Imagens* a, Imagens* b){
    if(a->getPriority() != b->getPriority())
        return a->getPriority() < b->getPriority();
//    else
//        return a->getScore() > b->getScore();
}
vector<Imagens*> SimmilarityClustering::eliminateDuplicatesAndSelectBetter(vector<Imagens*> grouping,int simThreshold){
    
    vector<Imagens*> selected;
     boost::numeric::ublas::matrix<double> mdistances = histComp->getSimilarities(grouping);
     Clustering::Points ps;
     //Doesn't matter the values, since the similarity is evaluated differently...
     Clustering::randomInit(ps, 10, grouping.size()); 
     
     Clustering::DBSCAN clusters(ps, simThreshold, 0);
     clusters.setSimilarity(mdistances);
     
     clusters.run_cluster();
     vector<vector<int> > groups = clusters.getClusters(clusters);
     
     
     for(int a=0;a<groups.size();a++){
         vector<Imagens*> sameCluster;
         vector<int> pids = groups[a];
         //pick up images whose ids are in this cluster
         for(int b=0;b<pids.size();b++){
             int id = pids[b];
             Imagens* img = grouping[id];
             sameCluster.push_back(img);
         }
         //order such images by its quality
         sort(sameCluster.begin(),sameCluster.end(),SimmilarityClustering::compareImagens);
         //Add best images...
         selected.push_back(sameCluster.at(0));
         //...and others that might interest.
//         for(int c=1;c<sameCluster.size();c++){
//             if(sameCluster[c]->getPriority()==0){
//                 selected.push_back(sameCluster[c]);
//             }
//         }
     }
//     cout << clusters;
//     cout.flush();
     
     return selected;
     
}


}